package com.supermarket_project.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.supermarket_project.common.R;
import com.supermarket_project.mapper.CategoryMapper;
import com.supermarket_project.mapper.GoodsMapper;
import com.supermarket_project.pojo.Category;
import com.supermarket_project.pojo.Goods;
import com.supermarket_project.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Goods)表服务实现类
 *
 * @author makejava
 * @since 2022-07-08 16:08:39
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public R addGoods(Goods goods) {
        Integer categoryId;
        if (goods.getSecondName() == null){
            categoryId = categoryMapper.selectCategoryByCategoryName(goods.getPrimaryName());
        }else {
            categoryId = categoryMapper.selectCategoryByCategoryName(goods.getSecondName());
        }
        goods.setGCategoryId(categoryId);
        goodsMapper.addGoods(goods);
        return R.success(null,"添加成功！");
    }

    @Override
    public R pageGoods(int pageNum, int pageSize, Integer gCategoryId, String goodsName, String barcode) {
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goodsList = goodsMapper.pageGoods(gCategoryId,goodsName,barcode);
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList, 5);
        List<Goods> list = goodsPageInfo.getList();
        List<Goods> goods = list.stream()
                .map(goods1 -> {
                    Category category = categoryMapper.queryCategory(goods1.getGCategoryId());
                    if (category.getFatherId() == null) {
                        goods1.setPrimaryName(category.getCategoryName());
                    } else {
                        goods1.setSecondName(category.getCategoryName());
                        Category fatherCategory = categoryMapper.queryCategory(category.getFatherId());
                        goods1.setPrimaryName(fatherCategory.getCategoryName());
                    }
                    return goods1;
                }).collect(Collectors.toList());
        goodsPageInfo.setList(goods);
        return R.success(goodsPageInfo,"操作成功");
    }

    @Override
    public R updateGoods(Goods goods) {
        goodsMapper.updateGoods(goods);
        return R.success(null,"修改商品信息成功！");
    }

    @Override
    public R queryGoods(int goodsId) {
        Goods goods = goodsMapper.queryGoods(goodsId);
        return R.success(goods,"操作成功");
    }

    @Override
    public R deleteGoods(int goodsId) {
        goodsMapper.deleteGoods(goodsId);
        return R.success(null,"删除成功");
    }

    @Override
    public R lockGoods(int goodsId, int status) {
        goodsMapper.lockGoods(goodsId,status);
        if(status == 1){
            return R.success(null,"取消商品锁定成功");
        }
        return R.success(null,"锁定商品成功");
    }

    @Override
    public List<Goods> queryGoodsByCategoryId(Integer gCategoryId) {
        return goodsMapper.queryGoodsByCategoryId(gCategoryId);
    }
}

